package cn.iocoder.yudao.module.expert.controller.admin.expertinfo;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.expert.controller.admin.expertinfo.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.expertinfo.ExpertInfoDO;
import cn.iocoder.yudao.module.expert.service.expertinfo.ExpertInfoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 专家信息")
@RestController
@RequestMapping("/expert/info")
@Validated
public class ExpertInfoController {

    @Resource
    private ExpertInfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建专家信息")
    @PreAuthorize("@ss.hasPermission('expert:info:create')")
    public CommonResult<Long> createInfo(@Valid @RequestBody ExpertInfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专家信息")
    @PreAuthorize("@ss.hasPermission('expert:info:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody ExpertInfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专家信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('expert:info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专家信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('expert:info:query')")
    public CommonResult<ExpertInfoRespVO> getInfo(@RequestParam("id") Long id) {
        ExpertInfoDO info = infoService.getInfo(id);
        return success(BeanUtils.toBean(info, ExpertInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专家信息分页")
    @PreAuthorize("@ss.hasPermission('expert:info:query')")
    public CommonResult<PageResult<ExpertInfoRespVO>> getInfoPage(@Valid ExpertInfoPageReqVO pageReqVO) {
        PageResult<ExpertInfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpertInfoRespVO.class));
    }

    @GetMapping("/getExpertByMajor")
    @Operation(summary = "通过专业获得专家信息")
    public CommonResult<List<ExpertInfoRespVO>> getInfoAll(@RequestParam("expertMajor") String expertMajor) {
        List<ExpertInfoDO> pageResult = infoService.getAllExpertInfo(expertMajor);
        return success(BeanUtils.toBean(pageResult, ExpertInfoRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出专家信息 Excel")
    @PreAuthorize("@ss.hasPermission('expert:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid ExpertInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpertInfoDO> list = infoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专家信息.xls", "数据", ExpertInfoRespVO.class,
                        BeanUtils.toBean(list, ExpertInfoRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入专家模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<ExpertInfoImportExcelVO> list =Arrays.asList(
                ExpertInfoImportExcelVO.builder().expertName("专家姓名").expertUnit("专家单位").expertPhone("13111111111").expertMajor("计算机").major("java")
                        .expertTitle("高级研究员").expertIden("JZ000001").remark("该条为示例").build()
        );
        // 输出
        ExcelUtils.write(response, "专家信息导入模板.xls", "专家列表", ExpertInfoImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入专家")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('expert:info:import')")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<ExpertInfoImportExcelVO> list = ExcelUtils.read(file, ExpertInfoImportExcelVO.class);
        return success(infoService.importExpertInfoList(list));
    }
}